package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.BrandDao;
import com.aaa.vegetablemarketexpress.entity.Brand;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.BrandService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @FileName: BrandServiceImpl
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/6 14:50
 * @Version: 1.0.0
 */

@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandDao brandDao;
    @Resource
    private HttpSession session; // 获取session

    /**
     * 分页查询
     * @param page
     * @return
     */
    @Override
    public PageInfo<Brand> queryByPage(Page<Brand> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageSize == null||pageNo == 0||pageSize == 0) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }
        // 调用PageHelper设置参数方法，开始分页
        PageHelper.startPage(pageNo,pageSize);
        // 获取page类的 data 属性，该属性 是Dept类型
        Brand data = page.getData();
        // 调用dao层查询
        List<Brand> brandList = brandDao.queryByParam(data);
        // 使用查询结果封装成返回对象
        return new PageInfo(brandList);
    }

    /**
     *  id查询品牌
     * @param brandId
     * @return
     */
    @Override
    public Brand queryByBrandId(Long brandId) {
        return brandDao.queryByBrandId(brandId);
    }

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @Override
    public int add(Brand brand) {
        brand.setStatus("0");
        brand.setDelFlag("0");
        brand.setCreateTime(new Date());
          User userList = (User)session.getAttribute("userInfo");
         String userName = userList.getUserName();
        brand.setCreateBy(userName);
        return brandDao.add(brand);
    }

    /**
     * 更新
     * @param brand
     * @return
     */
    @Override
    public int update(Brand brand) {
        brand.setUpdateTime(new Date());
        User userList = (User)session.getAttribute("userInfo");
        String userName = userList.getUserName();
        brand.setUpdateBy(userName);
        return brandDao.update(brand);
    }

    /**
     * 删除
     * @param brandId
     * @return
     */
    @Override
    public int deleteByBrandId(Long brandId) {
        return brandDao.deleteByBrandId(brandId);
    }
}
